package com.oraclejava.database;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class DatabaseEx2Activity extends Activity {
	SQLiteDatabase md;
    SimpleAdapter ma;
    ArrayList<String> al;
    ArrayAdapter<String> am;
    ListView lv;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize();
        createDatabase(md);
        
    }
	
	private void createDatabase(SQLiteDatabase md){
		
	}
	
	private void initialize(){
		setContentView(R.layout.main);
		String str = "";
		
		md = this.openOrCreateDatabase("PhoneBook", MODE_PRIVATE, null);
        md.execSQL("Drop table if exists members");
        
        md.execSQL("Create table members (" + 
        			"_id integer primary key autoincrement, " +
        			"Name text not null, " +
        			"Phone_No text not null);");
        
        md.execSQL("Insert into members " +
        			" (Name, Phone_No) values ('kdhong', '011-1111-1111');");
        
        ContentValues iv = new ContentValues();
        iv.put("Name","Juliet");
        iv.put("Phone_No", "010-111-1111");
        md.insert("members", null, iv);
        
        Cursor ar = md.query("members", null, null, null, null, null, null, null);
        
       
        al = new ArrayList<String>();
        if(ar != null){
        	if(ar.moveToFirst()){
        		do{
        			str = ar.getString(1) + "\t\t"+ar.getString(2);
        			al.add(str);
        		}while(ar.moveToNext());
        	}
        }
        am = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, al);
        
        lv = (ListView)findViewById(R.id.listView1);
        lv.setAdapter(am);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        if(md != null) md.close();

	}
	
	
}